<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="入库检查号 原主键:">
        <el-input v-model="queryParams.checkNo" size="small" placeholder="请输入入库检查号 原主键" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库编号:">
        <el-input v-model="queryParams.warehouseCode" size="small" placeholder="请输入仓库编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="域:">
        <el-input v-model="queryParams.siteCode" size="small" placeholder="请输入域" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="查询区域:">
        <el-input v-model="queryParams.locationSearch" size="small" placeholder="请输入查询区域" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="查询客户:">
        <el-input v-model="queryParams.customerSearch" size="small" placeholder="请输入查询客户" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="查询物料:">
        <el-input v-model="queryParams.materialSearch" size="small" placeholder="请输入查询物料" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.createBy" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-date-picker v-model="queryParams.createDate" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择" clearable />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.remark" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.approveBy" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.approveIdea" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-date-picker v-model="queryParams.approveDate" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择" clearable />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.version" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="更新时间:">
        <el-date-picker v-model="queryParams.updateDate" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择更新时间" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tWhMaterialCheck:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tWhMaterialCheck:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tWhMaterialCheck:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tWhMaterialCheck:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库检查号 原主键" align="center" prop="checkNo" />
      <el-table-column label="仓库编号" align="center" prop="warehouseCode" />
      <el-table-column label="域" align="center" prop="siteCode" />
      <el-table-column label="查询区域" align="center" prop="locationSearch" />
      <el-table-column label="查询客户" align="center" prop="customerSearch" />
      <el-table-column label="查询物料" align="center" prop="materialSearch" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="" align="center" prop="createBy" />
      <el-table-column label="" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="remark" />
      <el-table-column label="" align="center" prop="approveBy" />
      <el-table-column label="" align="center" prop="approveIdea" />
      <el-table-column label="" align="center" prop="approveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="version" />
      <el-table-column label="记录 ID，系统自动产生" align="center" prop="id" />
      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['web:tWhMaterialCheck:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['web:tWhMaterialCheck:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="tWhMaterialCheckFormDialog.title" :visible.sync="tWhMaterialCheckFormDialog.visible" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="tWhMaterialCheckForm" :model="tWhMaterialCheckFormData" :rules="tWhMaterialCheckFormRules" label-width="80px">
        <el-form-item label="入库检查号 原主键" prop="checkNo">
          <el-input v-model="tWhMaterialCheckFormData.checkNo" placeholder="请输入入库检查号 原主键" />
        </el-form-item>
        <el-form-item label="仓库编号" prop="warehouseCode">
          <el-input v-model="tWhMaterialCheckFormData.warehouseCode" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="域" prop="siteCode">
          <el-input v-model="tWhMaterialCheckFormData.siteCode" placeholder="请输入域" />
        </el-form-item>
        <el-form-item label="查询区域" prop="locationSearch">
          <el-input v-model="tWhMaterialCheckFormData.locationSearch" placeholder="请输入查询区域" />
        </el-form-item>
        <el-form-item label="查询客户" prop="customerSearch">
          <el-input v-model="tWhMaterialCheckFormData.customerSearch" placeholder="请输入查询客户" />
        </el-form-item>
        <el-form-item label="查询物料" prop="materialSearch">
          <el-input v-model="tWhMaterialCheckFormData.materialSearch" placeholder="请输入查询物料" />
        </el-form-item>
        <el-form-item label="" prop="createDate">
          <el-date-picker v-model="tWhMaterialCheckFormData.createDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择" clearable />
        </el-form-item>
        <el-form-item label="" prop="remark">
          <el-input v-model="tWhMaterialCheckFormData.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="approveBy">
          <el-input v-model="tWhMaterialCheckFormData.approveBy" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="approveIdea">
          <el-input v-model="tWhMaterialCheckFormData.approveIdea" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="approveDate">
          <el-date-picker v-model="tWhMaterialCheckFormData.approveDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择" clearable />
        </el-form-item>
        <el-form-item label="" prop="version">
          <el-input v-model="tWhMaterialCheckFormData.version" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker v-model="tWhMaterialCheckFormData.updateDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择更新时间" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmTWhMaterialCheckFormDialog">确 定</el-button>
        <el-button @click="resetTWhMaterialCheckFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TWhMaterialCheckApi from '@/api/web/TWhMaterialCheckApi'

const defaultTWhMaterialCheckFormData = {
  checkNo: null,
  warehouseCode: null,
  siteCode: null,
  locationSearch: null,
  customerSearch: null,
  materialSearch: null,
  status: '0',
  createDate: null,
  approveBy: null,
  approveIdea: null,
  approveDate: null,
  version: null,
  updateDate: null
}
export default {
  name: 'TWhMaterialCheckList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        checkNo: null,
        warehouseCode: null,
        siteCode: null,
        locationSearch: null,
        customerSearch: null,
        materialSearch: null,
        status: null,
        createBy: null,
        createDate: null,
        remark: null,
        approveBy: null,
        approveIdea: null,
        approveDate: null,
        version: null,
        updateDate: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      tWhMaterialCheckFormDialog: {
        title: '',
        visible: false
      },
      tWhMaterialCheckFormData: Object.assign({}, defaultTWhMaterialCheckFormData),
      tWhMaterialCheckFormRules: {
        checkNo: [{ required: true, message: '入库检查号 原主键不能为空', trigger: 'blur' }],
        warehouseCode: [{ required: true, message: '仓库编号不能为空', trigger: 'blur' }],
        siteCode: [{ required: true, message: '域不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
        createBy: [{ required: true, message: '不能为空', trigger: 'blur' }],
        createDate: [{ required: true, message: '不能为空', trigger: 'blur' }],
        version: [{ required: true, message: '不能为空', trigger: 'blur' }],
        updateDate: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openTWhMaterialCheckFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TWhMaterialCheckApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openTWhMaterialCheckFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      TWhMaterialCheckApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TWhMaterialCheckApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openTWhMaterialCheckFormDialog(id) {
      this.resetTWhMaterialCheckFormDialog()
      this.tWhMaterialCheckFormDialog.visible = true
      if (id) {
        this.tWhMaterialCheckFormDialog.title = '盘点-修改'
        TWhMaterialCheckApi.get(id).then(response => {
          this.tWhMaterialCheckFormData = {
            ...this.tWhMaterialCheckFormData,
            ...response.data
          }
        })
      } else {
        this.tWhMaterialCheckFormDialog.title = '盘点-添加'
      }
    },
    confirmTWhMaterialCheckFormDialog() {
      this.$refs['tWhMaterialCheckForm'].validate(valid => {
        if (valid) {
          if (this.tWhMaterialCheckFormData.id) {
            TWhMaterialCheckApi.edit(this.tWhMaterialCheckFormData).then(response => {
              this.$message.success('修改成功')
              this.tWhMaterialCheckFormDialog.visible = false
              this.handleQuery()
            })
          } else {
            TWhMaterialCheckApi.add(this.tWhMaterialCheckFormData).then(response => {
              this.$message.success('添加成功')
              this.tWhMaterialCheckFormDialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetTWhMaterialCheckFormDialog() {
      this.tWhMaterialCheckFormDialog = {
        title: '',
        visible: false
      }
      this.tWhMaterialCheckFormData = Object.assign({}, defaultTWhMaterialCheckFormData)
      this.resetForm('tWhMaterialCheckForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
